package com.ctgu.springshiro.dao;

import com.ctgu.springshiro.entity.ItemKill;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ItemKillDao {
    @Select("SELECT" +
            "       a.*," +
            "       b.name AS itemName," +
            "       (" +
            "        CASE WHEN (now() BETWEEN a.start_time AND a.end_time AND a.total > 0)" +
            "          THEN 1" +
            "        ELSE 0" +
            "        END" +
            "       )  AS canKill" +
            "   FROM item_kill AS a LEFT JOIN item AS b ON b.id = a.item_id" +
            "   WHERE a.is_active = 1")
    @Results(id = "BaseResultMap", value = {
            @Result(property = "id", column = "id"),
            @Result(property = "itemId", column = "item_id"),
            @Result(property = "total", column = "total"),
            @Result(property = "startTime", column = "start_time"),
            @Result(property = "endTime", column = "end_time"),
            @Result(property = "isActive", column = "is_active"),
            @Result(property = "createTime", column = "create_time"),
    })
    List<ItemKill> selectAll();

    @Select("SELECT" +
            "      a.*," +
            "      b.name AS itemName," +
            "      (" +
            "        CASE WHEN (now() BETWEEN a.start_time AND a.end_time AND a.total > 0)" +
            "          THEN 1" +
            "        ELSE 0" +
            "        END" +
            "      ) AS canKill" +
            "    FROM item_kill AS a LEFT JOIN item AS b ON b.id = a.item_id" +
            "    WHERE a.is_active = 1 AND a.id = #{id}")
    @ResultMap(value = {"BaseResultMap"})
    ItemKill selectById(@Param("id") Integer id);

    @Update("UPDATE item_kill " +
            "   SET total = total - 1 " +
            "   WHERE id = #{killId}")
    int updateKillItem(@Param("killId") Integer killId);

    @Select("SELECT" +
            "      a.*," +
            "      b.name AS itemName," +
            "      (CASE WHEN (now() BETWEEN a.start_time AND a.end_time)" +
            "        THEN 1" +
            "       ELSE 0" +
            "       END)  AS canKill" +
            "    FROM item_kill AS a LEFT JOIN item AS b ON b.id = a.item_id" +
            "    WHERE a.is_active = 1 AND a.id = #{id} AND a.total > 0")
    ItemKill selectByIdV2(@Param("id") Integer id);

    @Update("UPDATE item_kill " +
            "   SET total = total - 1 " +
            "   WHERE id = #{killId} AND total > 0")
    int updateKillItemV2(@Param("killId") Integer killId);
}